package org.example.MQ.util.QuartzConfig;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import org.example.MQ.rabbitmq.config.ConnectionUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.context.annotation.Configuration;



/**
 * @Author : zhangbo
 * @data : 2024/4/12
 * @description :
 */
@Slf4j
@Configuration
public class PublisherExecute implements Job {
    private final static String QUEUE_NAME = "q_test_01";
    @Override
    public void execute(JobExecutionContext jobExecutionContext){
        try {
            log.info("开始定时任务--send");
            send();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void send() throws Exception {
        //1、获取连接
        Connection connection = ConnectionUtil.getConnection();
        //2、声明信道
        Channel channel = connection.createChannel();
        //3、声明(创建)队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //4、定义消息内容
        String message = "------hello rabbitmq------ ";
        //5、发布消息
        channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
        log.info("发送消息"+message);
        //6、关闭通道
        channel.close();
        //7、关闭连接
        connection.close();
    }
}
